const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

/** 
 * 时间戳转化为年 月 日 时 分 秒 
 * number: 传入时间戳 
 * format：返回格式，支持自定义，但参数必须与formateArr里保持一致 
*/
function formatTimeTwo(number, format) {

  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 防止多次点击 start
function throttle(fn, gapTime) {
  if (gapTime == null || gapTime == undefined) {
    gapTime = 1500
  }
  let _lastTime = null
  // 返回新的函数
  return function () {
    let _nowTime = + new Date()
    console.log(_nowTime - _lastTime, gapTime, _lastTime)
    if (_nowTime - _lastTime > gapTime || !_lastTime) {
      fn.apply(this, arguments)   //将this和参数传给原函数
      _lastTime = _nowTime
    }
  }
}
// 防止多次点击 end

// 提示
function msg(msg, time, success) {
  wx.showToast({
    title: msg,
    icon: "none",
    duration: time || 2000,
    mask: false,
    success: success
  })
} 

//金额验证
function isMoney(money) {
  var reg = /^(\-?(0|[1-9]\d{0,})((\.[0-9]{1,2})?))$/;
  if (reg.test(money)) {
    return true;
  } else {
    return false;
  };
}

//验证大于等于0正整数
function valZe(input) {
  var regex = /^(0|[1-9][0-9]*)$/;
  return regex.test(input)
}

// 截取文件后缀
function suffixStr(urlName){
  let fileName = urlName.lastIndexOf(".");//取到文件名开始到最后一个点的长度
  let fileNameLength = urlName.length;//取到文件名长度
  let fileFormat = urlName.substring(fileName + 1, fileNameLength);//截
  return fileFormat
}

/**
 * 处理富文本里的图片宽度自适应
 * 1.去掉img标签里的style、width、height属性
 * 2.img标签添加style属性：max-width:100%;height:auto
 * 3.修改所有style里的width属性为max-width:100%
 * 4.去掉<br/>标签
 * @param html
 * @returns {void|string|*}
 */
function formatRichText(html){
  let widthFlag = false
  let heightFlag = false
  let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
    if(!match.match(/style="[^"]+"/gi)){
      var regex1 = new RegExp("(i?)(\<img)(?!(.*?style=['\"](.*)['\"])[^\>]+\>)","gmi");
      //给不含style="" 或 style='' 的img标签加上style=""
		  match = match.replace(regex1, "$2 style=\"display:block;\"$3");
    }
    if(match.match(/width="[^"]+"/gi)){
      widthFlag = true
    }
    if(match.match(/height="[^"]+"/gi)){
      heightFlag = true
    }
    return match
  });
  newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
    var s = ''
    if(!match.match(/width:[^;]+;/gi)){
      if(!widthFlag){
        s = s+'max-width:100%;'
      }
    }
    if(!match.match(/height:[^;]+;/gi)){
      if(!heightFlag){
        s = s+'height:auto;'
      }
    }
    var a = match.substr(0,match.length-1)
    var b = match.substr(match.length-1,match.length)
    var c = a+s+b
    return c;
  });
  return newContent;
}

module.exports = {
  formatTime: formatTime,
  formatTimeTwo: formatTimeTwo,
  throttle:throttle,
  msg:msg,
  isMoney:isMoney,
  formatRichText: formatRichText,
  suffixStr:suffixStr,
  valZe:valZe
}
